using EOSDigital.SDK;
using LogLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;

namespace EOSDigital.API
{
	public static class ErrorHandler
	{
		public static List<ErrorCode> NonSevereErrors
		{
			get;
			private set;
		}

		public static event SDKExceptionHandler NonSevereErrorHappened;

		public static event GeneralExceptionHandler SevereErrorHappened;

		static ErrorHandler()
		{
			ErrorHandler.NonSevereErrors = new List<ErrorCode>
			{
				ErrorCode.TAKE_PICTURE_AF_NG,
				ErrorCode.TAKE_PICTURE_CARD_NG,
				ErrorCode.TAKE_PICTURE_CARD_PROTECT_NG,
				ErrorCode.TAKE_PICTURE_LV_REL_PROHIBIT_MODE_NG,
				ErrorCode.TAKE_PICTURE_MIRROR_UP_NG,
				ErrorCode.TAKE_PICTURE_MOVIE_CROP_NG,
				ErrorCode.TAKE_PICTURE_NO_CARD_NG,
				ErrorCode.TAKE_PICTURE_NO_LENS_NG,
				ErrorCode.TAKE_PICTURE_SENSOR_CLEANING_NG,
				ErrorCode.TAKE_PICTURE_SILENCE_NG,
				ErrorCode.TAKE_PICTURE_SPECIAL_MOVIE_MODE_NG,
				ErrorCode.TAKE_PICTURE_STROBO_CHARGE_NG,
				ErrorCode.LENS_COVER_CLOSE,
				ErrorCode.DEVICE_BUSY,
				ErrorCode.PROPERTIES_UNAVAILABLE
			};
		}

		public static void CheckError(object sender, ErrorCode errorCode)
		{
			if (errorCode != 0)
			{
				bool flag = !ErrorHandler.NonSevereErrors.Any((ErrorCode t) => t == errorCode);
				SDKExceptionHandler nonSevereErrorHappened = ErrorHandler.NonSevereErrorHappened;
				if (!flag)
				{
					flag = (nonSevereErrorHappened == null);
				}
				if (flag)
				{
					throw new SDKException(errorCode);
				}
				nonSevereErrorHappened.BeginInvoke(sender, errorCode, delegate(IAsyncResult a)
				{
					AsyncResult asyncResult = a as AsyncResult;
					SDKExceptionHandler sDKExceptionHandler = asyncResult.AsyncDelegate as SDKExceptionHandler;
					sDKExceptionHandler.EndInvoke(a);
				}, null);
			}
		}

		public static void CheckError(ErrorCode errorCode)
		{
			if (errorCode == ErrorCode.OK)
			{
				return;
			}
			throw new SDKException(errorCode);
		}

		public static int CheckError(int countOrError)
		{
			if (countOrError == -1)
			{
				throw new SDKException(ErrorCode.INVALID_HANDLE);
			}
			return countOrError;
		}

		public static int CheckError(object sender, int countOrError)
		{
			return ErrorHandler.CheckError(countOrError);
		}

		public static bool ReportError(object sender, Exception ex)
		{
			try
			{
				GeneralExceptionHandler severeErrorHappened = ErrorHandler.SevereErrorHappened;
				if (severeErrorHappened == null)
				{
					return false;
				}
				severeErrorHappened.BeginInvoke(sender, ex, delegate(IAsyncResult a)
				{
					AsyncResult asyncResult = a as AsyncResult;
					GeneralExceptionHandler generalExceptionHandler = asyncResult.AsyncDelegate as GeneralExceptionHandler;
					generalExceptionHandler.EndInvoke(a);
				}, null);
				return true;
			}
			catch (Exception ex2)
			{
				Log.WriteExceptionAsync(ex2.ToString());
				Console.WriteLine(ex2.Message);
			}
			return true;
		}
	}
}
